//
//  YZCVideoViewController.m
//  LOLerBox
//
//  Created by 杨智超 on 16/1/16.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "YZCVideoViewController.h"
#import "YZCVideoCollectionCell.h"
#import "YZCAFNetwork.h"
#import "YZCVideoModel.h"
@interface YZCVideoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UISegmentedControl *segmentedControl;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *dataArr;

@end

@implementation YZCVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    /* 调用方法 */
    [self createCollectionView];
    [self createSegmented];
    [self handleData];
    /* 关闭交互 */
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"视频";
}

/* 获取数据 */
- (void)handleData {
    [YZCAFNetwork getUrl:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?src=duowan&action=c&sk=&sn=&pn=" body:nil response:YZCJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        for (NSDictionary *dic in resposeObject) {
            NSArray *arr = [dic objectForKey:@"subCategory"];
            self.dataArr = [YZCVideoModel modelHandlerWithArray:arr];
            [self.collectionView reloadData];
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"获取数据失败");
    }];
    
}

     


/* 创建CollctionView */
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    /* 横向滑动 */
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    /* 设置item大小 */
    layout.itemSize = CGSizeMake(375, 667 - 64 - 50 - 49);
    
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    
    /* 设置collectionView */
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + 50, 375, 667 - 64 - 50 - 49) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor cyanColor];
    /* 添加视图 */
    [self.view addSubview:self.collectionView];
    /* 设置代理人 */
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    /* 整页翻动 */
    self.collectionView.pagingEnabled = YES;
    /* 关闭边缘弹动 */
    self.collectionView.bounces = NO;
    /* 内存管理 */
    [_collectionView release];
    [layout release];
    [self.collectionView registerClass:[YZCVideoCollectionCell class] forCellWithReuseIdentifier:@"cell"];

}


/* collectionView协议方法 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}


/* cell设置 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YZCVideoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    /* 属性传值 */
    cell.dataArr = self.dataArr;
    
    return cell;
}
/* collectionView滚动方法 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collectionView) {
        if (self.collectionView.contentOffset.x == 0) {
            self.segmentedControl.selectedSegmentIndex = 0;
        }if (self.collectionView.contentOffset.x == 375) {
            self.segmentedControl.selectedSegmentIndex = 1;
        }if (self.collectionView.contentOffset.x == 375 * 2) {
            self.segmentedControl.selectedSegmentIndex = 2;
        }

    }
}

/* 创建按钮栏 */
- (void)createSegmented {
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"分类导航", @"排行榜", @"最新视频"]];
    self.segmentedControl.frame = CGRectMake(0, 64, self.view.frame.size.width, 50);
    /* 文字颜色 */
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [self.segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];
    /* 点击后颜色 */
    self.segmentedControl.tintColor = [UIColor colorWithWhite:0.251 alpha:1.000];
    [self.view addSubview:self.segmentedControl];
    self.segmentedControl.selectedSegmentIndex = 0;
    /* 背景颜色 */
    self.segmentedControl.backgroundColor = [UIColor blackColor];
    [self.segmentedControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [_segmentedControl release];

}

/* 按钮栏点击方法 */
- (void)segAction:(UISegmentedControl *)seg {
    /* 点击第几个按钮 */
    NSInteger index = seg.selectedSegmentIndex;
    /* collectionView偏移量 */
    CGFloat set = self.collectionView.contentOffset.x;
    switch (index) {
        case 0:
            set = 0;
            break;
        case 1:
            set = 375;
            break;
        default:
            set = 375 * 2;
            break;
    }
    
    [self.collectionView setContentOffset:CGPointMake(set, 0) animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
